package com.changwu.cwbase.AppTest.DesignPattern;

/**
 * Created by changwu on 2018/10/13.
 */

public class BuilderDesign {

    /**
     * Hero, the class with many parameters.
     */
    public static class Person {
        private String name;
        private int age;
        private boolean sex;

        public String getName() {
            return name;
        }

        public int getAge() {
            return age;
        }

        public boolean isSex() {
            return sex;
        }


        private Person(Builder builder) {
            name = builder.name;
            age = builder.age;
            sex = builder.sex;
        }

        public static class Builder {
            private String name;
            private int age;
            private boolean sex;

            public Builder name(String n) {
                name = n;
                return this;
            }

            public Builder age(int a) {
                age = a;
                return this;
            }

            public Builder sex(boolean s) {
                sex = s;
                return this;
            }

            public Person build() {
                return new Person(this);
            }
        }
    }


    public void testBuilderDesign() {
        Person person = new Person.Builder()
                .name("StephenHe")
                .age(20)
                .sex(true)
                .build();
        System.out.println(person.getName() + "\n" + person.getAge() + "\n" + person.isSex());
    }

}
